<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="拍卖方式" prop="auctionNumber">
        <el-select
          v-model="queryParams.auctionNumber"
          placeholder="请选择拍卖方式"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.AUCTION_METHOD)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="小区名称" prop="cellName">
        <el-input
          v-model="queryParams.cellName"
          placeholder="请输入小区名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

<!--      <el-form-item label="区" prop="district">-->
<!--        <el-input-->
<!--          v-model="queryParams.district"-->
<!--          placeholder="请输入区"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="图片" prop="isImage">
        <el-select
          v-model="queryParams.isImage"
          placeholder="请选择图片是否为空"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.HOUSING_IMAGE_NULL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="数据来源" prop="isHutai">
        <el-select
          v-model="queryParams.isHutai"
          placeholder="请选择数据来源"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.HOUSING_SOURCE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="同步新增时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          checkPermi="['housing:resource:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          checkPermi="['housing:resource:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="primary"
          plain
          @click="dialogVisible = true"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 客服电话
        </el-button>

        <el-popover
          placement="bottom"
          title="同步时间"
          :width="200"
          trigger="click"
          :content="contentTime"
        >
          <template #reference>
            <el-button class="m-2" @click="getHousingTime" >查看商业同步时间</el-button>
          </template>
        </el-popover>

      </el-form-item>
    </el-form>
  </ContentWrap>

  <el-dialog
    v-model="dialogVisible"
    title="客服电话"
    width="500"
  >
    <el-input v-model="formData.mobile" placeholder="请输入电话" />
    <template #footer>
      <div class="dialog-footer">

        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="handleClose">
          修改
        </el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
<!--      <el-table-column label="ID" align="center" prop="id" width="150px" />-->
      <el-table-column label="封面" align="center"  width="150px" >
        <template #default="scope">
          <img :src="scope.row.backgroundPic" style="width: 60px;height: 60px;border-radius: 4px;">
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" width="200px" />
      <el-table-column label="面积" align="center" prop="area" width="120px" />
<!--      <el-table-column label="房型" align="center" prop="houseType" width="150px" />-->
      <el-table-column label="房源类型" align="center" prop="housingType" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HOUSING_TYPE" :value="scope.row.housingType" />
        </template>
      </el-table-column>
      <el-table-column label="拍卖方式" align="center" prop="auctionNumber" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.AUCTION_METHOD" :value="scope.row.auctionNumber" />
        </template>
      </el-table-column>
      <el-table-column label="市场价" align="center" prop="marketPrice" width="120px" />
      <el-table-column label="市场单价" align="center" prop="marketUnitPrice" width="120px" />
      <el-table-column label="捡漏空间价格" align="center" prop="pickupSpacePrice" width="120px" />
      <el-table-column label="起拍价" align="center" prop="startingPrice" width="120px" />
      <el-table-column label="起拍单价" align="center" prop="startingOddPrice" width="120px" />
      <el-table-column label="折扣" align="center" prop="discount" width="120px" />
      <el-table-column label="小区名称" align="center" prop="cellName" width="120px" />
      <el-table-column label="梯步" align="center" prop="step" width="120px" />
      <el-table-column label="楼层" align="center" prop="floor" width="120px" />
      <el-table-column label="物业类型" align="center" prop="propertyType" width="120px" />
      <el-table-column label="拍卖次数" align="center" prop="numberOfAuctions" width="120px" />
      <el-table-column label="保证金" align="center" prop="earnestMoney" width="120px" />
      <el-table-column label="加价幅度" align="center" prop="bidIncrement" width="120px" />
      <el-table-column label="折扣率" align="center" prop="discountRate" width="120px" />
      <el-table-column label="建筑年份" align="center" prop="architectureYear" width="120px" />
      <el-table-column label="装修情况" align="center" prop="decorateSituation" width="120px" />
      <el-table-column label="省市区" align="center" prop="address" width="120px" />
      <el-table-column label="经度" align="center" prop="longitude" width="120px" />
      <el-table-column label="纬度" align="center" prop="latitude" width="120px" />
      <el-table-column
        label="新增时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="150px"
      />
      <el-table-column label="操作" align="center" width="150px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            checkPermi="['housing:resource:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            checkPermi="['housing:resource:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改1 -->
  <HousingResourceForm ref="formRef" @success="getList" style="height: 600px;width: 80%;overflow: auto;" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as HousingResourceApi from '@/api/hutai/housingResource'
import HousingResourceForm from './HousingResourceForm.vue'
import { ElMessage } from "element-plus";


defineOptions({ name: 'HousingResource' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const contentTime = ref('')
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  title: null,
  area: null,
  houseType: null,
  housingType: 2,
  auctionNumber: null,
  marketPrice: null,
  marketUnitPrice: null,
  pickupSpacePrice: null,
  startingPrice: null,
  startingOddPrice: null,
  discount: null,
  cellName: null,
  step: null,
  floor: null,
  propertyType: null,
  numberOfAuctions: null,
  earnestMoney: null,
  bidIncrement: null,
  discountRate: null,
  architectureYear: null,
  decorateSituation: null,
  address: null,
  district: null,
  longitude: null,
  latitude: null,
  imagesList: null,
  isHutai: null,
  isImage: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const formData = ref({
  mobile: undefined,
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await HousingResourceApi.getHousingResourcePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await HousingResourceApi.deleteHousingResource(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await HousingResourceApi.exportHousingResource(queryParams)
    download.excel(data, '房源信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const dialogVisible = ref(false)

const handleClose = (d: () => void) => {
  updateMobile()
  // ElMessageBox.confirm('确定变更提交吗?')
  //   .then(() => {
  //
  //   })
  //   .catch(() => {
  //     // catch error
  //   })
}

const getHousingTime =  async () => {
  const data = await HousingResourceApi.getHousingUpdateTime('2')
  contentTime.value = data
  //console.log(data)
}


const updateMobile = async () => {
  await  HousingResourceApi.updateHousingMobile(formData.value)
  ElMessage({
    type: 'success',
    message: '操作成功',
  })
  dialogVisible.value = false
}

const getHousingMobile = async () => {
  const  mobile = await  HousingResourceApi.getHousingMobile()
  formData.value.mobile = mobile;
  console.log(mobile);
}

/** 初始化 **/
onMounted(() => {
  getList()
  getHousingMobile()
})
</script>
<style lang="scss">
.el-dialog__title {
  line-height: 40px;
  margin-left: 20px;
}
</style>
